package com.ah.ajax_json;
import java.io.*;
import java.util.*;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonByJacksonConsole {
	@Test
	public void t1_Java2Json() {
		People p = new People();
		p.setName("甘宁");
		p.setPower(87);
		// 创建Jackson核心对象
		ObjectMapper mapper = new ObjectMapper();
		// 转换
		try {
			// 1.转为字符串
			String json = mapper.writeValueAsString(p);
			System.out.println("转为字符串:" + json);
			// 2.写入流（不一定是文件）
			mapper.writeValue(new FileWriter("json.txt"), p);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Test
	public void t2_属性注解() throws JsonProcessingException {
		// 对日期类型进行测试，默认json中的日期类型是毫秒值
		// @JsonIgnore忽视
		// @JsonFormat格式化
		class P2 {
			private Date day1;
			@JsonIgnore
			private Date day2;
			@JsonFormat(pattern = "yyyy/MM/dd")
			private Date day3;
			public Date getDay1() {
				return day1;
			}
			public void setDay1(Date day1) {
				this.day1 = day1;
			}
			public Date getDay2() {
				return day2;
			}
			public void setDay2(Date day2) {
				this.day2 = day2;
			}
			public Date getDay3() {
				return day3;
			}
			public void setDay3(Date day3) {
				this.day3 = day3;
			}
		}
		P2 p = new P2();
		p.setDay1(new Date());
		p.setDay2(new Date());
		p.setDay3(new Date());
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(p);
		System.out.println("属性注解:" + json);
	}
	@Test
	public void t3_Map2Json() throws JsonProcessingException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "诸葛亮");
		map.put("power", 20);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(map);
		System.out.println("Map转为字符串:" + json);
	}
	@Test
	public void t4_List2json() throws JsonProcessingException {
		List<People> ps = new ArrayList<People>();
		ps.add(new People("张辽", 93));
		ps.add(new People("徐晃", 94));
		ps.add(new People("张郃", 91));
		ps.add(new People("于禁", 90));
		ps.add(new People("乐进", 87));
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(ps);
		System.out.println("列表转为Json：" + json);
	}
	@Test
	public void t5_json2Java() throws JsonParseException, JsonMappingException,
			IOException {
		// 1.从字符串读取
		String json = "";
		// Jackson不认单引号：json = "{'name':'吕奉先','power':100}";
		// Jackson不认无引号：json = "{name:'吕奉先',power:100}";
		json = "{\"name\":\"吕奉先\",\"power\":100}";
		ObjectMapper mapper = new ObjectMapper();
		People p = mapper.readValue(json, People.class);
		System.out.println("Json转为对象（字符串）：" + p);
		// 2.从文件读取
		p = mapper.readValue(new FileReader("json.txt"), People.class);
		System.out.println("Json转为对象（文件）：" + p);
	}
	@Test
	public void t6_json2JavaList() throws JsonParseException,
			JsonMappingException, IOException {
		List<People> ps = new ArrayList<People>();
		ps.add(new People("张辽", 93));
		ps.add(new People("徐晃", 94));
		ps.add(new People("张郃", 91));
		ps.add(new People("于禁", 90));
		ps.add(new People("乐进", 87));
		ObjectMapper mapper = new ObjectMapper();
		// 得到一个json字符串
		String jsonList = mapper.writeValueAsString(ps);
		// 转为Java列表：类型为new TypeReference<List<People>>() {}
		List<People> ps2 = mapper.readValue(jsonList,
				new TypeReference<List<People>>() {});
		for (People p : ps2) {
			System.out.println("Json字符串转为Java列表：" + p);
		}
	}
}